package com.endoc.news.fragment.base;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ViewModel;

import com.endoc.news.fragment.homefragment.NewsChannelsBeanModel;
import com.endoc.news.fragment.mvvm.model.IBaseModelListener;
import com.endoc.news.fragment.mvvm.model.PagingResult;
import com.endoc.news.fragment.newslist.NewsListBeanModel;
import com.endoc.news.fragment.newslist.view.ViewStatus;
import com.endoc.news.netWoke.bean.NewsChannelsBean;

import java.util.ArrayList;
import java.util.List;

//继承IBaseModelListener,实现所有数据传输是来自model本身
public abstract class BaseMvvmViewModel<MODEL extends BaseMvvmModel,DATA> extends ViewModel implements LifecycleObserver, IBaseModelListener<List<DATA>> {
   public MutableLiveData<List<DATA>> listMutableLiveData = new MutableLiveData<>();
    MODEL mvvmModel;//ViewModel持有model
    public MutableLiveData<String> errorStringLiveData = new MutableLiveData<>();
    public MutableLiveData<ViewStatus> mViewStatusMutableLiveData = new MutableLiveData<>();

    public abstract MODEL createModel();

    public void refresh() {
        createNotNullModel();
        try {
            mvvmModel.refresh();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void createNotNullModel(){
        if(mvvmModel ==null){
            mvvmModel = createModel();
        }
        if(mvvmModel !=null){
            mvvmModel.registerModelListener(this);
        }
    }

    //得到数据
    public void getCache(){
        createNotNullModel();
        if(mvvmModel !=null){
            mvvmModel.getCacheData();
        }
    }

    public void loadNext() {
        createNotNullModel();
        try {
            mvvmModel.loadNextPage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //当前页面gg了,那么对应的订阅也取消
    @Override
    protected void onCleared() {
        super.onCleared();
        if(mvvmModel!=null){
            mvvmModel.cancel();
        }
    }


    @Override
    public void success(BaseMvvmModel baseMvvmModel, List<DATA> t, PagingResult... pagingResults) {
        if(listMutableLiveData.getValue()==null){
            listMutableLiveData.setValue(new ArrayList<DATA>());
        }
        if(baseMvvmModel instanceof NewsListBeanModel){
            if(pagingResults[0].isFirstPage){//如果是第一页
                listMutableLiveData.getValue().clear();
            }
            if(pagingResults[0].isEmpty){//如果为空,说明没有数据,解析失败
                if(pagingResults[0].isFirstPage){//如果是第一页,说明没有一页有数据
                    mViewStatusMutableLiveData.postValue(ViewStatus.EMPTY);
                }else {
                    //如果是其他页,说明没有更多的数据了
                    mViewStatusMutableLiveData.postValue(ViewStatus.NO_MORE_DATA);
                }
            }else {
                if(pagingResults[0].isFirstPage){//如果是第一页,那么直接用这次数据
                    listMutableLiveData.postValue(t);
                }else {//如果不是第一页
                    listMutableLiveData.getValue().addAll(t);//加入集合
                    listMutableLiveData.postValue(listMutableLiveData.getValue());
                }
            }
        }
        else if(baseMvvmModel instanceof NewsChannelsBeanModel){
            listMutableLiveData.postValue(t);
            //notifyListener(newsChannelsBean,newsChannelsBean.showapiResBody.channelList,isFromCache);
        }

      /*  if(pagingResults!=null&&pagingResults.length>0&&pagingResults[0].isFirstPage){
            mBaseViewModels.clear();
        }
        mBaseViewModels.addAll(t);
        //((NewsRecycleAdapter)fragmentNewsBinding.recycler.getAdapter()).setData(baseViewModelList);
        ((NewsRecycleAdapter)fragmentNewsBinding.recycler.getAdapter()).setData(mBaseViewModels);
        fragmentNewsBinding.srl.finishLoadMore();
        fragmentNewsBinding.srl.finishRefresh();*/
    }

    @Override
    public void fail(BaseMvvmModel baseMvvmModel, Throwable e, PagingResult... pagingResults) {
        errorStringLiveData.postValue(e.getMessage());
        if(pagingResults[0].isFirstPage){//如果是第一页,说明没有一页有数据
            mViewStatusMutableLiveData.postValue(ViewStatus.REFRESH_ERROR);
        }else {
            //如果是其他页,说明没有更多的数据了
            mViewStatusMutableLiveData.postValue(ViewStatus.NO_MORE_DATA);
        }

       /* fragmentNewsBinding.srl.finishLoadMore();
        fragmentNewsBinding.srl.finishRefresh();*/
    }


    //注册一个在onResume就执行的操作
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private void onResume(){
        //如果存在数据
        if(listMutableLiveData!=null&&listMutableLiveData.getValue()!=null&&listMutableLiveData.getValue().size()>0){
            listMutableLiveData.postValue(listMutableLiveData.getValue());
        }else {//如果不存在
                createModel();//创建对象
                getCache();//得到缓存
        }
    }

}
